<?php
include_once("../Dao/db.php");
Class SiteEdit
{
	var $errorMessage;
	
	var $siteInfoList="";
	
	var $siteInfoCount;
	
	var $fromToPage;
	
	var $nowPageNumber; 
	
	var $pageNums;

	var $rowCountHtml;	
	
	// 毎ページのレコード数
	var $RECORD_NUMBER_PER_PAGE = 20;
	
	function mytask() {
		if ($_POST["act"] == "insert") {
			session_start();
			$_SESSION["siteId"] = "";
			header("Location:SiteEditDetail.php");
		} else if ($_POST["act"] == "edit") {
			session_start();
			$_SESSION["siteId"] = $_POST["paramSiteId"];
			header("Location:SiteEditDetail.php");
		} else if ($_POST["act"] == "delete") {
			$this->doDelete();		
			if ($_GET["nowPageNumber"] !=null && $_GET["nowPageNumber"] != "") {
				$this->nowPageNumber = $_GET["nowPageNumber"] ;
			} else {			
				$this->nowPageNumber = 1;
			}
		// 初期化と削除ボタンを押下時
		} else {
			if ($_GET["nowPageNumber"] !=null && $_GET["nowPageNumber"] != "") {
				$this->nowPageNumber = $_GET["nowPageNumber"] ;
			} else {			
				$this->nowPageNumber = 1;
			}					
		} 
		// 検索処理を行う
		$this->doSearch();
	}
	
	function doSearch()
	{
		$this->getSiteInfoList();
	}
	
	function getSiteInfoList()
	{ 
		$fromPageNumber = ($this->nowPageNumber-1) * $this->RECORD_NUMBER_PER_PAGE;
		// データレコード数を取得
		$this->getSiteInfoCount();
		// サイト情報を取得sql文
		$sql = "select siteId, siteName,siteUrl,content,genreId,pageView,uniqueUser from siteinfo where memberId='"
							.$_SESSION["memberId"]."'";
//$sql = "select siteId, siteName,siteUrl,content,genreId,pageView,uniqueUser from SiteInfo ";
		$sql = $sql." order by siteId";
		$toPageNumber = $fromPageNumber  + $this->RECORD_NUMBER_PER_PAGE;
		$sql = $sql." limit ".$fromPageNumber.",".$this->RECORD_NUMBER_PER_PAGE;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);

		if ($rows > 0)
		{
			$this->rowCountHtml = "<input type='hidden' id='rowCount' name='rowCount' value='".$rows."'>";
			// value='".$row."'>";
			$fromPageNumber = $fromPageNumber + 1;
			$toPageNumber  = $fromPageNumber + $rows -1;
			$this->fromToPage = $fromPageNumber."件-".$toPageNumber."件";
			for ($i=0;$i<$rows;$i++)
			{  
				$this->siteInfoList  = $this->siteInfoList. " <tr class='tr4' > " ;
				$data = mysql_fetch_row($result);
				for ($j=0;$j<8;$j++) {

					$colValue = $data[$j];
					if ($j==0) {						
						$this->siteInfoList  = $this->siteInfoList. "<td align='center'>".$colValue."</td>" ; 
					} else if ($j==1) {
						$this->siteInfoList  = $this->siteInfoList. " <td width='14%' align='left'>
							<input type='button' class='btn_ico btn_edit' title='編集' 
							value='' onclick='doEditClick(".$data[0].")'>";
						$this->siteInfoList  = $this->siteInfoList.$colValue."</td>" ;
					} else if($j==2 || $j==3) {
						$this->siteInfoList  = $this->siteInfoList. "<td align='left'>".$colValue."</td>" ;
					} else if ($j==4) {
						$colValue = $this->getGenreName($colValue);
						$this->siteInfoList  = $this->siteInfoList. "<td align='left'>".$colValue."</td>" ;
					} else if($j==5 || $j==6) {
						$this->siteInfoList  = $this->siteInfoList. "<td align='right'>".$colValue."</td>" ;
					} else {
						$this->siteInfoList  = $this->siteInfoList. "<td align='center'>"."
								<input id='bodyCheckBox".$i."' name='bodyCheckBox".$i."' type='checkbox' value='".$data[0]."' /></td>";
					}
				}
				$this->siteInfoList  = $this->siteInfoList."</tr>";
			}			
		} else if ($this->nowPageNumber > 1) {
			$this->nowPageNumber = $this->nowPageNumber - 1;
			$this->doSearch();
		} else {
			$this->errorMessage  =  "データが存在していません、ご確認ください。";
		}
	}

	function  getSiteInfoCount() {
		$sql = "select count(siteId) from siteinfo where memberId='".$_SESSION["memberId"]."'";
		//$sql = "select count(siteId) from SiteInfo";
		$result = mysql_query($sql);		
		$cnt = mysql_fetch_row($result);
		if ($cnt[0] == 0) {
			return;
		}
		$this->siteInfoCount =  "/".$cnt[0]."件"; 
		$this->nowPage  = $this->nowPage."</td><td>ページ </td>";
		for ($i=0;$i < $cnt[0]/$this->RECORD_NUMBER_PER_PAGE;$i++) {
			$page = $i + 1;
			if ($this->nowPageNumber == $page) {
				$this->pageNums = $this->pageNums."<td width='10'>
						<a href='SiteEdit.php?nowPageNumber=".$page;
				$this->pageNums = $this->pageNums."'>"."[".$page."]";
			} else {
				$this->pageNums = $this->pageNums."<td width='10'>
						<a href='SiteEdit.php?nowPageNumber=".$page;
				$this->pageNums = $this->pageNums."'>".$page;
			}
			$this->pageNums = $this->pageNums."</a></td>";
		}
	}

	function doDelete() {
		// レコード数を取得
		if ( $_POST["rowCount"] == 0) {
			// エラーメッセージを表示する
			$this->errorMessage = "削除したいデータを選択ください。";
			return;
		}
		$deleteCount = 0;
		// すべてのチェックボックスを繰り戻す
		for($i=0;$i < $_POST["rowCount"];$i++) {
			$checkBoxId = "bodyCheckBox".$i;
			// 選択されましたデータ
			if ($_POST[$checkBoxId] !=null && $_POST[$checkBoxId] != "") {
				$siteId = $_POST[$checkBoxId];	
				$sql = "delete from siteinfo where siteId='".$siteId."'";
				mysql_query($sql);	
				$deleteCount++;
			}		
		}
		if ($deleteCount == 0) {
			$this->errorMessage = "削除したいデータを選択ください。";
			return;
		}
	}

	function getGenreName($genreId) {
		$sql = "select genreName from genreinfo where genreId='".$genreId."'";
		//$sql = "select count(siteId) from SiteInfo";
		$result = mysql_query($sql);		
		$rows = mysql_num_rows($result);
		if ($rows == 0) {
			return "";
		} else {
			$genreNameData = mysql_fetch_row($result);
			return $genreNameData[0];
		}
	}
}
?>